<?php
/**
 * PackageOrderController is the default controller for this application
 */
class BillingController extends ControllerBase {
	/**
	 * PackageOrderDataService object
	 *
	 * @var PackageOrderDataService object
	 */
	public $packageOrderDataService = null;
	
	/**
	 * UsersDataService Object
	 *
	 * @var UsersDataService object
	 */
	public $usersDataService = null;
	
	//User ID
	public $userId = NULL;
	
	/**
	 * Authentication object
	 * @var Authentication object
	 */
	public $authentication = null;
	
	public function __construct() {
		parent::__construct ();
		
		// Init Authentication class
		$this->authentication = new Authentication ();
		
		// Only administrator can right
		if ($this->authentication->isAdministrator () == false) {
			
			// Go to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		$this->packageOrderDataService = new PackageOrderDataService ();
		
		// New Users Data Service
		$this->usersDataService = new UsersDataService ();
		
		$this->userId = $this->authentication->getCurrentUserId ();
		
		// Set Page name
		$this->setPageName ( "billing" );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		$this->latestAction ();
	}
	
	/**
	 * Get Latest Package Purchased
	 */
	public function latestAction() {
		// Geting all latest purchase products
		$arrElement = array ();
		$arrElement ['status'] = PackageOrderEntity::COMPLETED;
		
		$numRecordPerPage = 30;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		$totals = $this->packageOrderDataService->getTotals ( $arrElement );
		$arrLatestPurchasedPackageEntity = $this->packageOrderDataService->getItems ( $arrElement, 'createdDate DESC', $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Billing/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'itemList', $arrLatestPurchasedPackageEntity );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Billing/latest.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Handling Expired Billing Action
	 */
	public function expiredAction() {
		$arrElement = array ();
		//$arrElement['status'] = PackageOrderEntity::COMPLETED;
		$arrElement [] = " (status = '" . PackageOrderEntity::COMPLETED . "' OR status = '" . PackageOrderEntity::CANCELLED . "' )";
		$now = date ( "Y-m-d H:i:s" );
		$arrElement [] = " expiredDate <= '{$now}'";
		
		$numRecordPerPage = 30;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		$totals = $this->packageOrderDataService->getTotals ( $arrElement );
		$arrLatestPurchasedPackageEntity = $this->packageOrderDataService->getItems ( $arrElement, 'createdDate DESC', $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Billing/expired";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'pageLink', $pageLink );
		$this->smarty->assign ( 'itemList', $arrLatestPurchasedPackageEntity );
		$this->smarty->assign ( 'status', "Expired" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Billing/expired.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Handling Active Billing Action
	 */
	public function activeAction() {
		// Geting all latest purchase products
		$arrElement = array ();
		//$arrElement['status'] = PackageOrderEntity::COMPLETED;
		$arrElement [] = " (status = '" . PackageOrderEntity::COMPLETED . "' OR status = '" . PackageOrderEntity::CANCELLED . "' )";
		$now = date ( "Y-m-d H:i:s" );
		$arrElement [] = " expiredDate > '{$now}'";
		
		$numRecordPerPage = 30;
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		$totals = $this->packageOrderDataService->getTotals ( $arrElement );
		$arrLatestPurchasedPackageEntity = $this->packageOrderDataService->getItems ( $arrElement, 'createdDate DESC', $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/Billing/active";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		$this->smarty->assign ( 'itemList', $arrLatestPurchasedPackageEntity );
		$this->smarty->assign ( 'status', "Active" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Billing/active.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a order
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		
		// Get Id of order
		$id = $this->getPost ( 'idValue', '' );
		
		// Delete a order item
		$res = $this->packageOrderDataService->del ( $id );
		
		// View order list
		$this->latestAction ();
	}
	
	/**
	 * Delete list of order
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteallAction() {
		$iCount = $this->getPost ( 'iCount', 0 );
		for($i = 0; $i < $iCount; $i ++) {
			$id = $this->getPost ( 'checkItem' . $i, '' );
			if (! empty ( $id )) {
				
				// Delete a order item
				$this->packageOrderDataService->del ( $id );
			}
		}
		
		// View order list
		$this->latestAction ();
	}
	
	/**
	 * Update status list of order
	 *
	 * @access private
	 *
	 * @param int $status: Status of the product order
	 *
	 * @return void
	 */
	protected function updateStatus($status) {
		// Get action key
		$actionKey = $this->getPost ( 'actionKey', '' );
		if (! isset ( $_SESSION ['tokenKey'] ) || $actionKey == $_SESSION ['tokenKey']) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					$this->packageOrderDataService->updateStatus ( $id, $status );
				}
			}
		}
	}
	
	/**
	 * Get User Name od a Userid
	 *
	 * @access public
	 *
	 * @param int $userId: User ID
	 *
	 * @return string
	 */
	public function getUserName($userId) {
		$str = "";
		if ($userId != "") {
			$userItem = $this->usersDataService->getById ( $userId );
			if (isset ( $userItem )) {
				$str = $userItem->username;
			}
		}
		
		return $str;
	}
	public function test() {
		echo '123';
	}
}
